package jmetal.metaheuristics.moead.dynamic;

import java.util.HashMap;

import com.sun.istack.internal.logging.Logger;

import jmetal.core.Algorithm;
import jmetal.core.Operator;
import jmetal.core.Problem;
import jmetal.core.SolutionSet;
import jmetal.operators.crossover.CrossoverFactory;
import jmetal.operators.mutation.MutationFactory;
import jmetal.problems.ddtlz.F1;
import jmetal.problems.ddtlz.F2;
import jmetal.problems.ddtlz.F3;
import jmetal.problems.ddtlz.F4;
import jmetal.util.JMException;

public class DynamicMEAD_main {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) throws JMException, ClassNotFoundException {
	    Problem   problem   ;         // The problem to solve
	    Algorithm algorithm ;         // The algorithm to use
	    Operator  crossover ;         // Crossover operator
	    Operator  mutation  ;         // Mutation operator
	    HashMap  parameters ; // Operator parameters

	    problem = new F4(12,0);

	    algorithm = new DynamicMOEAD(problem);
//	    algorithm = new MOEADReduc(problem);
	    
	    // Algorithm parameters
	    algorithm.setInputParameter("populationSize",300);
	    algorithm.setInputParameter("maxEvaluations",300000);
	    algorithm.setInputParameter("T", 20) ;
	    algorithm.setInputParameter("delta", 0.9) ;
	    algorithm.setInputParameter("nr", 10) ;

	    // Crossover operator 
	    parameters = new HashMap() ;
	    parameters.put("CR", 1.0) ;//交叉概率
	    parameters.put("F", 0.5) ;
	    crossover = CrossoverFactory.getCrossoverOperator("DifferentialEvolutionCrossover", parameters);                   
	    
	    // Mutation operator
	    parameters = new HashMap() ;
	    parameters.put("probability", 1.0/problem.getNumberOfVariables()) ;
	    parameters.put("distributionIndex", 20.0) ;
	    mutation = MutationFactory.getMutationOperator("PolynomialMutation", parameters);                    
	    
	    algorithm.addOperator("crossover",crossover);
	    algorithm.addOperator("mutation",mutation);
	    
	    // Execute the Algorithm
	    long initTime = System.currentTimeMillis();
	    SolutionSet population = algorithm.execute();
	    long estimatedTime = System.currentTimeMillis() - initTime;
	    Logger logger = Logger.getLogger(DynamicMEAD_main.class);
	    // Result messages 
	    logger.info("Total execution time: "+estimatedTime + "ms");
	    logger.info("Objectives values have been writen to file FUN");
	    population.printObjectivesToFile("output_data/MOEAD_FUN_"+problem.getNumberOfObjectives());
	    logger.info("Variables values have been writen to file VAR");
	    population.printVariablesToFile("output_data/MOEAD_VAR_"+problem.getNumberOfObjectives());    
	}
          
  
}
